﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DalEntity
{
  public partial  class Motorista : Modelo.Interface.IBaseBD
    {
        ShippingEntities db;
        public Motorista()
        {
            db = Base.GetInstance();
        }
        public IList<T> RetornaGeral<T>() where T : class, new()
        {
            //db = Base.GetInstance();// new ShippingEntities(); 
            List<T> retorno = new List<T>();
            foreach (Motorista C in db.Motorista.ToList())
            {
                T valor = GenericTypeConverter.ConvertType<Motorista, T>(C);

                retorno.Add(valor);
            }
            return retorno;
        }

        public T RetornaBasePorID<T>(int id) where T : class, new()
        {
           // db = Base.GetInstance();
            return GenericTypeConverter.ConvertType<Motorista, T>(db.Motorista.SingleOrDefault(c => c.Id == id));
        }

        //public object RetornaInstancia<T>(T obj, ShippingEntities instancia ) where T : class, new()
        //{
        //    Motorista mot = GenericTypeConverter.ConvertType<T, Motorista>(obj);
      
        //    using (db = new ShippingEntities())
        //    {
                
        //        if (mot.IdTransportadora != null)
        //        {
        //            int id = mot.IdTransportadora.Value;
        //            mot.Transportadora = instancia.Transportadora.SingleOrDefault(c => c.Id == id);
                     
        //            //db.Motorista.AddObject(mot);
        //            //db.SaveChanges();
        //        }
        //    }

        //    return mot;
        //}

        public object RetornaInstancia<T>(T obj) where T : class, new()
        {
            Motorista mot = GenericTypeConverter.ConvertType<T, Motorista>(obj);
            if (mot.IdTransportadora != null)
              {
                  int id = mot.IdTransportadora.Value;
                  mot.Transportadora = db.Transportadora.SingleOrDefault(c => c.Id == id);
              }
           return mot;
        }
    }
}
